package com.weijianhuawen.wjblog.common.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.util.List;
import java.util.Objects;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-20 0:06
 */
@Data
public class PageResponse<T> extends Response<List<T>>{
    //总条数
    private Long total = 0L;
    //总页数
    private Long pages;
    //每一页记录数
    private Long size = 10L;
    //当前页码
    private Long pageIndex;

    public static <T> PageResponse<T> success(IPage page, List<T> data) {
        PageResponse<T> response = new PageResponse<>();
        response.setSuccess(true);
        response.setPageIndex(Objects.isNull(page) ? 1L : page.getCurrent());
        response.setSize(Objects.isNull(page) ? 10L : page.getSize());
        response.setPages(Objects.isNull(page) ? 0L : page.getPages());
        response.setTotal(Objects.isNull(page) ? 0L : page.getTotal());
        response.setData(data);

        return  response;
    }
    public static <T> PageResponse<T> success(Long current, Long size, Long total, List<T> data) {
        PageResponse<T> response = new PageResponse<>();
        response.setSuccess(true);
        response.setTotal(total);
        response.setSize(size);
        if (size != 0L) response.setPages((long)(Math.ceil((double) total/ size)));
        response.setPageIndex(current);
        response.setData(data);
        return response;
    }
}
